package com.sniper.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sniper.feign.service.ClientHiService;
import com.sniper.feign.service.HelloService;
import com.sniper.model.LineShop;
import com.sniper.mybatis.service.impl.LineShopService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author suzhen
 * @create 2018/3/14
 */
@RestController
@RequestMapping("/line-shop")
public class DbaIndexController {

    private static Logger logger = LoggerFactory.getLogger(DbaIndexController.class);
    @Autowired
    private LineShopService lineShopService;


    @Autowired
    private RestTemplate template;

    @Autowired
    private HelloService helloService;

    @Autowired
    private ClientHiService clientHiService;

    @RequestMapping(value = "hi", method = RequestMethod.GET)
    public String hi(@RequestParam("name") String name) {
        logger.debug("service-dba hi:{}", name);
        return clientHiService.hiService(name);
    }


    @RequestMapping(value = "hi2", method = RequestMethod.GET)
    public String hi2(@RequestParam("name") String name) {
        return "service-dba demo";
    }

    @HystrixCommand(fallbackMethod = "getError")
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public LineShop get(@RequestParam String id) {
        logger.debug("请求ID：{}", id);
        return lineShopService.get(id);
    }

    public LineShop getError(String id, Throwable throwable) {
        throwable.printStackTrace();
        LineShop lineShop = new LineShop();
        lineShop.setId("-1");
        return lineShop;
    }

    @HystrixCommand(fallbackMethod = "listError")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public List<LineShop> list(@RequestParam String id) {
        logger.debug("请求ID：{}", id);
        if (StringUtils.isNotBlank(id)) {
            if (id.contains(",")) {
                String[] ids = id.split(",");
                List<LineShop> lineShops = new ArrayList<>(ids.length);
                LineShop lineShop;
                for (String i : ids) {
                    if (StringUtils.isNotBlank(i)) {
                        lineShop = lineShopService.get(i);
                        if (lineShop != null) {
                            lineShops.add(lineShop);
                        }
                    }
                }
                return lineShops;
            }
            return Collections.singletonList(lineShopService.get(id));
        }
        return Collections.emptyList();
    }

    public List<LineShop> listError(String id) {
        return Collections.emptyList();
    }

}
